﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.Net;
using Microsoft.Samples.Kinect.SkeletonBasics;

    class Server
    {

        private bool isConnected = false;
        private String headXOrientation;

        private TcpListener clientProgramListener;
        private Thread clientListenThread;

        //for sending to main program
        private String currentJointInfo;
        private static String gestureInfo = "";
        private static Boolean isJumping = false;

        private List<KinectMachineThread> machines;

        private KinectNetworkManager knm;
        private GestureRecognition recognizer;
        private static NetworkStream clientStream;


        public Server()
        {

            machines = new List<KinectMachineThread>();

            Console.WriteLine("Creating TCP server");
            clientProgramListener = new TcpListener(IPAddress.Any, 1240);
            clientListenThread = new Thread(new ThreadStart(ListenForClientProgram));
            clientListenThread.Start();
            clientListenThread.IsBackground = true;

            knm = new KinectNetworkManager();

        }


        //creates TCP connection for client program
        //only use to communicate with VISIE application
        private void ListenForClientProgram()
        {

            clientProgramListener.Start();

            while (true)
            {

                TcpClient client = this.clientProgramListener.AcceptTcpClient();
                isConnected = true;
                clientStream = client.GetStream();
                Console.WriteLine("connected to main program");


                byte[] receivedMessage = new byte[4096];
                int bytesread;

                while (true)
                {

                    //bytesread = 0;

                    try
                    {
                        bytesread = clientStream.Read(receivedMessage, 0, 4096);
                    }
                    catch
                    {
                        Console.WriteLine("Client has disconnected");
                        break;
                    }

                    if (bytesread == 0)
                    {
                        Console.WriteLine("Client has disconnected");
                        break;
                    }

                    ASCIIEncoding encoder = new ASCIIEncoding();
                    byte[] sentMessage = new byte[0];


                    if (char.ConvertFromUtf32(receivedMessage[0]).Equals("0"))
                    {
                        currentJointInfo = knm.calculateJointMessage();

                        if (isJumping && !gestureInfo.Equals(""))
                        {
                            sentMessage = encoder.GetBytes("B" + currentJointInfo + "$" + gestureInfo + "%");
                            isJumping = false;
                            gestureInfo = "";
                        }
                        else if (isJumping)
                        {
                            Console.WriteLine("send jump");
                            sentMessage = encoder.GetBytes("J" + currentJointInfo);
                            isJumping = false;
                        }

                        else if (!gestureInfo.Equals(""))
                        {
                            sentMessage = encoder.GetBytes("G" + currentJointInfo + "$" + gestureInfo + "%");
                            gestureInfo = "";
                        }
                        else
                        {
                            sentMessage = encoder.GetBytes("N" + currentJointInfo);
                        }
 
                    }
                    clientStream.Write(sentMessage, 0, sentMessage.Length);
                    clientStream.Flush();
                }

                client.Close();

         }
        }

        public void calculateJointMessage()
        {
            currentJointInfo = knm.calculateJointMessage();
        }

        public void sendHeadOrientation(String s)
        {
            headXOrientation = s;
        }

        public void setGestureRecognizer(GestureRecognition gr) {
            recognizer = gr;
        }

        public static void setGestureString(String gesture) {
            if (gestureInfo.Equals(""))
            {
                gestureInfo = gesture;
            }

        }

        public static void setJump() {
            isJumping = true;
        }


    }
